package solutions.leetcode.normal


/**
 * @author lizhidong
 * <a href="https://leetcode.cn/leetbook/read/top-interview-questions/xm42hs/">189.旋转数组</a>
 */
class Solution189 {
    fun rotate(nums: IntArray, k: Int): Unit {
        val mid = nums.size - k % nums.size
        back(nums, 0, mid - 1)
        back(nums, mid, nums.size - 1)
        back(nums, 0, nums.size - 1)
    }

    private fun back(nums: IntArray, left: Int, right: Int) {
        var i = left
        var j = right
        while (i < j) {
            val t = nums[i]
            nums[i] = nums[j]
            nums[j] = t
            i++
            j--
        }
    }
}

fun main() {
    Solution189().rotate(arrayOf(
        -1, -100, 3, 99
    ).toIntArray(), 2)
}